<html>
<head>
<title>day05作业_接口-类=实现描述功能.ets</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
.s0 { color: #bbb529;}
.s1 { color: #a9b7c6;}
.s2 { color: #faa23d;}
.s3 { color: #db7e9b;}
.s4 { color: #52cb54;}
.s5 { color: #e2da90;}
.s6 { color: #aa7dfc;}
.s7 { color: #b3e54c;}
</style>
</head>
<body bgcolor="#2b2b2b">
<table CELLSPACING=0 CELLPADDING=5 COLS=1 WIDTH="100%" BGCOLOR="#606060" >
<tr><td><center>
<font face="Arial, Helvetica" color="#000000">
day05作业_接口-类=实现描述功能.ets</font>
</center></td></tr></table>
<pre><span class="s0">@</span><span class="s1">Entry</span>
<span class="s0">@</span><span class="s1">Component</span>
<span class="s1">struct Index </span><span class="s2">{</span>
  <span class="s1">build</span><span class="s3">() </span><span class="s2">{</span>
  <span class="s2">}</span>
<span class="s2">}</span>

<span class="s4">//定义接口</span>
<span class="s1">interface Iperson </span><span class="s2">{</span>
  <span class="s1">name</span><span class="s5">: </span><span class="s1">string</span>
  <span class="s1">age</span><span class="s5">: </span><span class="s1">number</span>
  <span class="s1">friends</span><span class="s5">: </span><span class="s1">string</span><span class="s3">[]</span>
<span class="s2">}</span>

<span class="s4">//定义枚举</span>
<span class="s1">enum StudentMale </span><span class="s2">{</span>
  <span class="s1">Boy </span><span class="s5">= </span><span class="s6">'男孩'</span><span class="s5">,</span>
  <span class="s1">Girl </span><span class="s5">= </span><span class="s6">'女孩'</span>
<span class="s2">}</span>

<span class="s4">//定义类</span>
<span class="s1">class Person </span><span class="s2">{</span>
  <span class="s1">name</span><span class="s5">: </span><span class="s1">string</span>
  <span class="s1">age</span><span class="s5">: </span><span class="s1">number</span>
  <span class="s1">friends</span><span class="s5">: </span><span class="s1">string</span><span class="s3">[]</span>

  <span class="s4">//构造函数，接收Iperson类型参数</span>
  <span class="s1">constructor</span><span class="s3">(</span><span class="s1">person</span><span class="s5">: </span><span class="s1">Iperson</span><span class="s3">) </span><span class="s2">{</span>
    <span class="s1">this</span><span class="s5">.</span><span class="s1">name </span><span class="s5">= </span><span class="s1">person</span><span class="s5">.</span><span class="s1">name</span>
    <span class="s1">this</span><span class="s5">.</span><span class="s1">age </span><span class="s5">= </span><span class="s1">person</span><span class="s5">.</span><span class="s1">age</span>
    <span class="s1">this</span><span class="s5">.</span><span class="s1">friends </span><span class="s5">= </span><span class="s1">person</span><span class="s5">.</span><span class="s1">friends</span>
  <span class="s2">}</span>

  <span class="s4">//方法</span>
  <span class="s1">sayHi</span><span class="s3">() </span><span class="s2">{</span>
    <span class="s1">console</span><span class="s5">.</span><span class="s1">log</span><span class="s3">(</span><span class="s6">'依次打印name,age,friends -----&gt; '</span><span class="s5">, </span><span class="s1">this</span><span class="s5">.</span><span class="s1">name</span><span class="s5">, </span><span class="s1">this</span><span class="s5">.</span><span class="s1">age</span><span class="s5">, </span><span class="s1">this</span><span class="s5">.</span><span class="s1">friends</span><span class="s3">)</span>
  <span class="s2">}</span>
<span class="s2">}</span>

<span class="s1">const person </span><span class="s5">= </span><span class="s1">new Person</span><span class="s3">(</span><span class="s2">{ </span><span class="s1">name</span><span class="s5">: </span><span class="s6">'李四'</span><span class="s5">, </span><span class="s1">age</span><span class="s5">: </span><span class="s7">19</span><span class="s5">, </span><span class="s1">friends</span><span class="s5">: </span><span class="s3">[</span><span class="s6">'王五'</span><span class="s5">, </span><span class="s6">'小二'</span><span class="s5">, </span><span class="s6">'张三'</span><span class="s5">, </span><span class="s6">'小红'</span><span class="s3">] </span><span class="s2">}</span><span class="s3">)</span>
<span class="s1">person</span><span class="s5">.</span><span class="s1">sayHi</span><span class="s3">()</span>

<span class="s4">//继承接口</span>
<span class="s1">interface IStudent extends Iperson </span><span class="s2">{}</span>

<span class="s4">//继承Person</span>
<span class="s1">class Student extends Person </span><span class="s2">{</span>
  <span class="s1">male</span><span class="s5">: </span><span class="s1">StudentMale</span>
  <span class="s1">grade</span><span class="s5">: </span><span class="s1">string</span>

  <span class="s1">constructor</span><span class="s3">(</span><span class="s1">male</span><span class="s5">: </span><span class="s1">StudentMale</span><span class="s5">, </span><span class="s1">grade</span><span class="s5">: </span><span class="s1">string</span><span class="s5">, </span><span class="s1">person1</span><span class="s5">: </span><span class="s1">IStudent</span><span class="s3">) </span><span class="s2">{</span>
    <span class="s1">super</span><span class="s3">(</span><span class="s1">person1</span><span class="s3">)</span>
    <span class="s1">this</span><span class="s5">.</span><span class="s1">male </span><span class="s5">= </span><span class="s1">male</span>
    <span class="s1">this</span><span class="s5">.</span><span class="s1">grade </span><span class="s5">= </span><span class="s1">grade</span>
  <span class="s2">}</span>

  <span class="s4">//子类方法</span>
  <span class="s1">sayHi</span><span class="s3">()</span><span class="s5">: </span><span class="s1">void </span><span class="s2">{</span>
    <span class="s1">super</span><span class="s5">.</span><span class="s1">sayHi</span><span class="s3">()</span>
    <span class="s1">console</span><span class="s5">.</span><span class="s1">log</span><span class="s3">(</span><span class="s6">'打印male'</span><span class="s5">, </span><span class="s1">StudentMale</span><span class="s5">.</span><span class="s1">Boy</span><span class="s3">)</span>
    <span class="s1">console</span><span class="s5">.</span><span class="s1">log</span><span class="s3">(</span><span class="s6">'打印grade'</span><span class="s5">, </span><span class="s1">this</span><span class="s5">.</span><span class="s1">grade</span><span class="s3">)</span>
  <span class="s2">}</span>
<span class="s2">}</span>

<span class="s1">const student </span><span class="s5">= </span><span class="s1">new Student</span><span class="s3">(</span><span class="s1">StudentMale</span><span class="s5">.</span><span class="s1">Boy</span><span class="s5">, </span><span class="s6">'优秀'</span><span class="s5">, </span><span class="s2">{ </span><span class="s1">name</span><span class="s5">: </span><span class="s6">'张三'</span><span class="s5">, </span><span class="s1">age</span><span class="s5">: </span><span class="s7">20</span><span class="s5">, </span><span class="s1">friends</span><span class="s5">: </span><span class="s3">[</span><span class="s6">'李四'</span><span class="s5">, </span><span class="s6">'王五'</span><span class="s5">, </span><span class="s6">'小二'</span><span class="s3">] </span><span class="s2">}</span><span class="s3">)</span>
<span class="s1">student</span><span class="s5">.</span><span class="s1">sayHi</span><span class="s3">()</span></pre>
</body>
</html>